package com.wanggy.mystore.controller;

import com.wanggy.mystore.controller.ex.*;
import com.wanggy.mystore.service.ex.*;
import com.wanggy.mystore.utils.JsonResult;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;

//用于统一处理控制层的异常
public class BaseController {

    //操作成功的状态码
    public static final int OK = 200;

    //头像上传大小显示
    public static final Long AVATAR_MAX_SIZE = 10*1024*1024L;

    //文件类型控制
    public static final ArrayList<String> AVATAR_TYPE = new ArrayList<>(Arrays.asList("image/png","image/jpeg"));

    /**
     * 1. @ExceptionHandler表示该方法用于处理捕获抛出的异常
     * 2. 什么的异常才会被这个方法处理呢？所以需要ServiceException.class,这样的话只要时抛出ServiceException异常就会被拦截到handleException方法，此时handleException方法就是处理请求处理方法，返回值就是需要传递给前端的数据
     * 3. 被ExceptionHandler修饰后如果项目发生异常，那么异常对象就会被自动传递给此方法的参数列表上，所以形参就需要写Throwable e用来接收异常对象
     */
    @ExceptionHandler({ServiceException.class,FileUploadException.class})
    public JsonResult<Void> handleException(Throwable e) {
        JsonResult<Void> result = new JsonResult<>();
        if (e instanceof UserNameDuplicatedException) {
            result.setState(4000);
            result.setMessage(e.getMessage());
        } else if (e instanceof InsertException) {
            result.setState(5000);
            result.setMessage(e.getMessage());
        } else if (e instanceof UserNotFoundException) {
            result.setState(4001);
            result.setMessage(e.getMessage());
        } else if (e instanceof PasswordNotMatchException) {
            result.setState(4002);
            result.setMessage(e.getMessage());
        }else if (e instanceof UpdateException) {
            result.setState(5001);
            result.setMessage(e.getMessage());
        }else if(e instanceof FileEmptyException){
            result.setState(6000);
            result.setMessage(e.getMessage());
        }else if(e instanceof FileSizeException){
            result.setState(6001);
            result.setMessage(e.getMessage());
        }else if(e instanceof FileTypeException){
            result.setState(6002);
            result.setMessage(e.getMessage());
        }else if(e instanceof FileStateExcetion){
            result.setState(6003);
            result.setMessage(e.getMessage());
        }else if(e instanceof FileUploadIoExcetion){
            result.setState(6004);
            result.setMessage(e.getMessage());
        }else if(e instanceof AddressCountLimitException){
            result.setState(4003);
            result.setMessage(e.getMessage());
        }else if(e instanceof AddressNotFoundException){
            result.setState(4004);
            result.setMessage(e.getMessage());
        }else if(e instanceof AccessDeniedException){
            result.setState(4005);
            result.setMessage(e.getMessage());
        }else if(e instanceof DeleteException){
            result.setState(5002);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 获取session对象中的uid
     * @param session session对象
     * @return 当前登录用户uid的值
     */
    public final Integer getUidFromSession(HttpSession session){
        //getAttribute返回的是Object对象，需要转换伟字符串在转换为包装类
        return Integer.valueOf(session.getAttribute("uid").toString());
    }

    /**
     * 获取session对象中username
     * @param session session对象
     * @return 当前登录用户username的值
     */
    public final String getUsernameFromSession(HttpSession session){
        return session.getAttribute("username").toString();
    }
}
